import sys
from colorama import init
from colorama import Fore, Back, Style
from robot import Robot
from room import Room
from map import Map
from evaluation import evaluator
import time
import os

# mode: 0 --- robot's map
# 		1 --- room's map
def draw(map, mode):
	init()
	assert isinstance(map, Map)
	if mode == 1:
		print '  - '*(map.row_size)
		for index in range(map.col_size):
			y = map.col_size-1-index
			print '|',
			for x in range(map.row_size):
				# robot
				value = map.map[x][y]
				if x ==map.robotX and y == map.robotY:
					if map.robotO is 0:
						print (Back.BLUE  + '^'),
					elif map.robotO is 1:
						print (Back.BLUE  + '>'),
					elif map.robotO is 2:
						print (Back.BLUE  + 'v'),
					else:
						print (Back.BLUE+'<'),
				# unknown
				elif value==-2:
					print (Back.BLACK+'?'),
				# obstacle
				elif value==-1:
					print (Back.RED+'x'),
				# free
				elif value==0:
					print (Back.GREEN+' '),
				# visited
				else:
					print value,
				print (Back.RESET+'|'),
			print " "
			print '  - '*(map.row_size)

""" ----------------------------------------------------------------------------- """
"""                                Script: Main Driver                             """
""" ----------------------------------------------------------------------------- """
evaluator = evaluator()
room = Room()
#True: with wall following, False: without wall following
robot = Robot(True)
start_time = time.clock()
while True:
	os.system('cls' if os.name == 'nt' else 'clear')
	draw(robot.map, 1)
	time.sleep(0.5)
	percepts = room.getPercepts()
	robot.updateMap(percepts)
	actions = robot.getNextAction()
	if len(actions) > 0:
		robotX, robotY, robotO = robot.executeActionSequence(actions)
		room.updateRobotLocation(robotX, robotY, robotO)
	else:
		break

print "  "
print robot.history

totalMove, optimalMove, accNotVisitied = evaluator.get_totalmove(robot.map)
timeUsed = time.clock()-start_time
print("{}{}".format("Time Used: ", timeUsed))
print("{}{}".format("Evaluator: total moves with wall following ", totalMove))
print("{}{}".format("Evaluator: optimal total moves ", optimalMove))
print("{}{}".format("Evaluator: accessible but not visited ", accNotVisitied))